<template>
    <el-container class="home_container">
        <el-header>
            <div>
                <img src="../assets/logo.png" alt="您要找的图片不在了">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体 -->
        <el-container>
            <!-- 侧边 -->
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu :unique-opened="true" background-color="#263238" text-color="#fff" active-text-color="#89DDFF" :collapse="isCollapse" 
                                :collapse-transition="false" router :default-active="activePath">
                    <el-submenu :index="item.id+''" v-for="(item, key) in menuList" :key="item.id">
                        <!-- 一级菜单模板 -->
                        <template slot="title">
                            <i :class="iconsObj[key]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                            <!-- 二级菜单模板 -->
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    </el-menu>
            </el-aside>
            <!-- 右侧主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            // 左侧菜单数据
            menuList: [],
            // 一级菜单id -> 图标
            iconsObj: [
                'el-icon-s-custom',
                'el-icon-s-check',
                'el-icon-s-shop',
                'el-icon-s-claim',
                'el-icon-s-marketing'
            ],
            // 是否折叠菜单
            isCollapse: false,
            // 被激活的链接地址
            activePath: ''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    watch: {
        $route(to, from) {
            if(to.path === '/welcome') {
                this.activePath = ''
            }  
        }
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有菜单数据
        async getMenuList() {
            const { data: result } = await this.$http.get('menus')
            if(result.meta.status !== 200) {
                return this.$message.error(result.meta.msg)
            }
            this.menuList = result.data
        },
        // 点击切换菜单折叠展开
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        },
        // 保存链接激活状态
        saveNavState(activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath = activePath
        }
    }
}
</script>

<style lang="less" scoped>
.home_container{
    height: 100%;
}
.el-menu{
    border-width: 0;
}
.el-header{
    background-color: #263238;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div{
        display: flex;
        align-items: center;
    }
    img{
        width: 30px;
        height: 30px;
        margin-right: 22px;
    }
}
.el-aside{
    background-color: #263238;
}
.el-main{
    background-color: #eaedf1;
}
.toggle-button{
    background-color: #2a373d;
    color: #fff;
    font-size: 12px;
    line-height: 30px;
    text-align: center;
    font-weight: 700;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
